from data_structures.stack.array_stack import ArrayStack

def postfix_to_res(postfix_expr):
    operandStack = ArrayStack()

    tokenList = postfix_expr.split()

    for token in tokenList:
        if token in "0123456789":
            operandStack.push(int(token))
        else:
            operand2 = operandStack.pop()  #符号右侧
            operand1 = operandStack.pop()  #符号左侧
            result = do_math(token, operand1, operand2)
            #print(result)
            operandStack.push(result)

    return operandStack.pop()

def do_math(op, op1, op2):
    if op  == "*":
        return op1 * op2
    elif op == "/":
        return op1 / op2
    elif op == "+":
        return op1 + op2
    else:
        return op1 - op2

if __name__ == "__main__":

    expression = "3 5 * 8 + 3 -"
    print("Postfix notation: " +  expression)
    print("result: " ,postfix_to_res(expression))
